package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.util.UserContext ;
import cn.wolfcode.trip.base.domain.PrivateMassage;
import cn.wolfcode.trip.base.query.PrivateMassageQuery;
import cn.wolfcode.trip.base.service.IPrivateMassageService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/privateMessages")
public class PrivateMessageController {
    @Autowired
    private IPrivateMassageService privateMassageService;
    /**
     * 查询所有消息
     * @return
     */
    @GetMapping
    public PageInfo listMassage(PrivateMassageQuery qo){
        //每页查询10条
        qo.setPageSize(10);
        qo.setUserId(UserContext.getCurrentUser().getId());
        qo.setOrderBy("pm.state desc,pm.createdate desc");
        return privateMassageService.query(qo);
    }
    /**
     * 更改私信消息读取状态
     * @return
     */
    @PutMapping
    public JSONResult changeState(Long id) {
        privateMassageService.changeState(id,false);
        return new JSONResult();
    }

    /**
     * 查询收到私信消息数量
     * @return
     */
    @GetMapping("/{id}")
    public int noSeen(@PathVariable Long id){
        return privateMassageService.queryMessageCount(id,PrivateMassage.STATE_NOSEEN);
    }

    @PostMapping
    public JSONResult save(PrivateMassage pm){
        //传入当前用户
        pm.setUser(UserContext.getCurrentUser());
        pm.setState(true);
        privateMassageService.save(pm);
        return new JSONResult();
    }

}
